package com.bw.module_main.view

import android.animation.ValueAnimator
import android.animation.ValueAnimator.AnimatorUpdateListener
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_common.arouter.Router


class CircleTimeView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private var mSweepAngle = 0 //扫描的弧度
    private var mAnimator: ValueAnimator? = null


    // 添加状态标志，用于控制跳转只发生一次
    private var isCountdownFinished = false

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE // 描边，绘制边框
        strokeWidth = 3f // 边框宽度，可根据需求调整
        color = Color.WHITE // 边框颜色
        strokeCap = Paint.Cap.ROUND // 圆角端点，使弧的两端更平滑
    }

    private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.WHITE // 文字颜色
        textSize = 40f // 文字大小，可根据需求调整
        textAlign = Paint.Align.CENTER // 文字水平居中对齐
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        // 计算圆形参数
        val radius = minOf(width, height) / 2f - paint.strokeWidth / 2
        val centerX = width - radius
        val centerY = radius

        // 绘制进度弧，从-90度（12点钟位置）开始，顺时针旋转
        val rectF = android.graphics.RectF(
            centerX - radius,
            centerY - radius,
            centerX + radius,
            centerY + radius
        )

        // 绘制动态进度弧，根据mSweepAngle值变化
        canvas.drawArc(rectF, -90f, mSweepAngle.toFloat(), false, paint)



        // 绘制倒计时文字
        val timeLeft = ((5000 - mSweepAngle * 5000 / 360) / 1000) + 1 // 计算剩余时间
        val fontMetrics = textPaint.fontMetrics
        val textY = centerY - (fontMetrics.descent + fontMetrics.ascent) / 2
        canvas.drawText("$timeLeft s", centerX, textY, textPaint)
        if(timeLeft == 1){
            ARouter.getInstance().build(Router.Ui.GuideActivity).navigation()
        }
    }


    fun start() {

        // 重置状态
        isCountdownFinished = false


        // 如果动画器为空，则创建一个新的
        if (mAnimator == null) {
            // 使用ofInt创建动画器，设置起始值和结束值
            mAnimator = ValueAnimator.ofInt(0, 360)
            mAnimator?.setDuration(5000) // 5秒

            // 添加动画更新监听器
            mAnimator?.addUpdateListener { animation ->
                mSweepAngle = animation.animatedValue as Int
                invalidate() // 刷新界面
            }
        }

        // 确保动画器不为空且未运行时才启动
        if (mAnimator != null && !mAnimator!!.isRunning) {
            mAnimator!!.start()
        }




    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        return super.onTouchEvent(event)
        when (event?.action) {
            MotionEvent.ACTION_UP -> {
                // 停止动画
                mAnimator?.cancel()
                // 跳转到引导页
                ARouter.getInstance().build(Router.Ui.GuideActivity).navigation()
            }

        }
    }
}